package com.ocom.mqsimple.business.wgkq;

import com.alibaba.fastjson.JSON;
import com.ocom.common.request.simple.ssgl.SSGLMqBaseRequest;
import com.ocom.common.request.simple.ssgl.SSGLNewPersonInfoRequest;
import com.ocom.mqsimple.config.WGKQMqConfig;
import com.ocom.mqsimple.feign.ExposeClientService;
import com.ocom.redis.util.RedisUtil;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.Map;


@Component
public class WGKQConsumerMqListener {

	private Logger logger = LoggerFactory.getLogger(WGKQConsumerMqListener.class);

	@Autowired
	ExposeClientService exposeClientService;


	/**
	 * 把该监听命名为A监听
	 * 监听队列  远程服务支付接口发送消息  该队列会接受
	 * [message, headers, channel]
	 * @return {@link }
	 * @throws
	 */
	@RabbitListener(queues = WGKQMqConfig.WGKQ_QUEUE)
	public void process(Message message, @Headers Map<String, Object> headers, Channel channel) throws Exception {
		logger.info("message【{}】",message);
		String ssglMessage = new String(message.getBody(), StandardCharsets.UTF_8);

		try {
			SSGLMqBaseRequest request = JSON.parseObject(ssglMessage, SSGLMqBaseRequest.class);

			SSGLNewPersonInfoRequest ssglNewPersonInfoRequest = JSON.parseObject(request.getMessage(), SSGLNewPersonInfoRequest.class);
			exposeClientService.wgkqNewPersonInfo(ssglNewPersonInfoRequest);
		} catch (Exception e) {
			logger.info("WGKQ-出现异常队列消息开始进入死信队列{{}}",e);
		}

		channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
	}

	/**
	 * 把该监听命名为B监听
	 * 监听死信队列  远程服务支付接口发送消息  该队列会接受
	 * [message, headers, channel]
	 * @return {@link }
	 * @throws
	 */
	@RabbitListener(queues =  WGKQMqConfig.WGKQ_DIE_QUEUE)
	public void processForDie(Message message, @Headers Map<String, Object> headers, Channel channel) throws Exception {
		//死信队列放弃
		channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
		logger.info("SSGL-死信队列放弃 ， 【{}】",message);
	}


}
